// 生成小程序所需本地图标的脚本
const fs = require('fs');
const { createCanvas } = require('canvas');
const path = require('path');

// 创建目录函数
function ensureDirectoryExists(dirPath) {
  if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath, { recursive: true });
  }
}

// 定义要创建的图标
const iconsToCreate = [
  // 首页分类图标
  { name: 'images/icons/category1.png', color: '#FF6B81', label: '女装' },
  { name: 'images/icons/category2.png', color: '#4CAF50', label: '男装' },
  { name: 'images/icons/category3.png', color: '#2196F3', label: '童装' },
  { name: 'images/icons/category4.png', color: '#FF9800', label: '鞋包' },
  { name: 'images/icons/category5.png', color: '#9C27B0', label: '配饰' },
  { name: 'images/icons/category6.png', color: '#E91E63', label: '美妆' },
  { name: 'images/icons/category7.png', color: '#795548', label: '家居' },
  { name: 'images/icons/category8.png', color: '#607D8B', label: '数码' },
  { name: 'images/icons/category9.png', color: '#F44336', label: '食品' },
  { name: 'images/icons/category10.png', color: '#673AB7', label: '全部' },
  { name: 'images/icons/all.png', color: '#333333', label: '全部' },
  
  // 搜索和顶部图标
  { name: 'images/icons/search.png', color: '#999999', symbol: '🔍' },
  { name: 'images/icons/top.png', color: '#999999', symbol: '↑' },
  { name: 'images/icons/empty.png', color: '#CCCCCC', symbol: '∅' },
  
  // 美妆分类图标
  { name: 'images/category/skincare.png', color: '#4CAF50', label: '护肤' },
  { name: 'images/category/makeup.png', color: '#E91E63', label: '彩妆' },
  { name: 'images/category/perfume.png', color: '#9C27B0', label: '香水' },
  { name: 'images/category/body.png', color: '#2196F3', label: '美体' },
  { name: 'images/category/tools.png', color: '#FF9800', label: '工具' },
  { name: 'images/category/sets.png', color: '#795548', label: '套装' }
];

// 创建纯色图标
function createIcon(filename, options) {
  const size = options.size || 64;
  const canvas = createCanvas(size, size);
  const ctx = canvas.getContext('2d');

  // 清空画布
  ctx.clearRect(0, 0, size, size);
  
  // 设置背景颜色透明
  ctx.fillStyle = 'rgba(0,0,0,0)';
  ctx.fillRect(0, 0, size, size);
  
  if (options.label) {
    // 绘制圆形背景
    ctx.beginPath();
    ctx.arc(size/2, size/2, size/2 - 2, 0, Math.PI * 2);
    ctx.fillStyle = options.color || '#FF6B81';
    ctx.fill();
    
    // 绘制文字
    ctx.font = `bold ${size/2}px Arial`;
    ctx.fillStyle = 'white';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText(options.label.substr(0, 1), size/2, size/2);
  } else if (options.symbol) {
    // 绘制符号
    ctx.font = `${size*0.7}px Arial`;
    ctx.fillStyle = options.color || '#999999';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText(options.symbol, size/2, size/2);
  }

  // 保存图标
  const dirPath = path.dirname(filename);
  ensureDirectoryExists(dirPath);
  
  const buffer = canvas.toBuffer('image/png');
  fs.writeFileSync(filename, buffer);
  console.log(`Created: ${filename}`);
}

// 创建简单横幅
function createBanner(filename, text) {
  const width = 750;
  const height = 200;
  const canvas = createCanvas(width, height);
  const ctx = canvas.getContext('2d');

  // 设置渐变背景
  const gradient = ctx.createLinearGradient(0, 0, width, 0);
  gradient.addColorStop(0, '#FF6B81');
  gradient.addColorStop(1, '#FF9CAA');
  ctx.fillStyle = gradient;
  ctx.fillRect(0, 0, width, height);
  
  // 添加文字
  ctx.font = 'bold 48px Arial';
  ctx.fillStyle = 'white';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillText(text || '商品分类', width/2, height/2);

  // 保存图片
  const dirPath = path.dirname(filename);
  ensureDirectoryExists(dirPath);
  
  const buffer = canvas.toBuffer('image/png');
  fs.writeFileSync(filename, buffer);
  console.log(`Created: ${filename}`);
}

// 创建购物车图标
function createCartIcon(filename) {
  const size = 200;
  const canvas = createCanvas(size, size);
  const ctx = canvas.getContext('2d');

  // 清空画布
  ctx.clearRect(0, 0, size, size);
  
  // 绘制购物车
  ctx.beginPath();
  
  // 购物车篮子
  ctx.moveTo(40, 80);
  ctx.lineTo(60, 150);
  ctx.lineTo(160, 150);
  ctx.lineTo(180, 80);
  ctx.closePath();
  
  ctx.strokeStyle = '#999999';
  ctx.lineWidth = 5;
  ctx.stroke();
  
  // 购物车轮子
  ctx.beginPath();
  ctx.arc(80, 170, 10, 0, Math.PI * 2);
  ctx.moveTo(150, 170);
  ctx.arc(140, 170, 10, 0, Math.PI * 2);
  ctx.fillStyle = '#999999';
  ctx.fill();
  
  // 购物车手柄
  ctx.beginPath();
  ctx.moveTo(40, 80);
  ctx.lineTo(30, 60);
  ctx.lineTo(20, 60);
  ctx.strokeStyle = '#999999';
  ctx.lineWidth = 5;
  ctx.stroke();
  
  // 添加文字
  ctx.font = 'bold 24px Arial';
  ctx.fillStyle = '#999999';
  ctx.textAlign = 'center';
  ctx.fillText('购物车空空如也', size/2, 40);

  // 保存图标
  const dirPath = path.dirname(filename);
  ensureDirectoryExists(dirPath);
  
  const buffer = canvas.toBuffer('image/png');
  fs.writeFileSync(filename, buffer);
  console.log(`Created: ${filename}`);
}

// 创建所有图标
console.log('开始创建本地图标...');

try {
  // 创建图标
  for (const icon of iconsToCreate) {
    createIcon(icon.name, {
      color: icon.color,
      label: icon.label,
      symbol: icon.symbol,
      size: 64
    });
  }
  
  // 创建横幅
  createBanner('images/default_banner.png', '时尚商城');
  
  // 创建购物车图标
  createCartIcon('images/cart-empty.png');
  
  console.log('所有图标创建完成！');
} catch (error) {
  console.error('创建图标时出错:', error);
  console.log('提示: 您需要安装node-canvas包才能运行此脚本:');
  console.log('npm install canvas');
} 